import {ModalAttributeType} from './form-attribute'
import {VNode} from "vue";

const modalTypeFilter = (type: string) => {
  let field: string = '';
  // 特殊弹窗可以做特殊处理
  switch (type) {
    case ModalAttributeType.RadioOption:
    case ModalAttributeType.CheckboxOption:
      field = 'defaultValue';
      break;
    case ModalAttributeType.RegexpFormula:
      field = 'regexpText';
      break;
  }
  return field;
};

/**
 *  导入
 * @param attr
 * @param attrs
 */

export const importModal = (attr: any, attrs: any) => {
  const field = modalTypeFilter(attr.attrType);
  let defaultField: any;
  if (!!field) {
    defaultField = attrs.find((item: any) => {
      return item.field === field;
    });
  }
  return {
    value: attr.value,
    default: defaultField ? defaultField.value : null,
    type: attr.options ? attr.options.modalType : null
  };
};
/**
 *输出
 * @param data
 * @param attr
 * @param attrs
 * @param callback
 */
export const exportModal = (data: any, attr: any, attrs: any, callback: Function, self: VNode) => {
  let field: string = modalTypeFilter(attr.attrType);
  if (!!field) {
    callback(field, data.default);
  }
  return data && data.value ? data.value : '';
};




